﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Diagnostics;

namespace DigitalSolutionLibrary.Utilities
{
    public class SendMail
    {
        private string authEmail;
        private string authPassword;
        private string frmEmail;
        private string frmName;
        private string toEmail;
        private string subject;
        private string content;

        /// <summary>
        /// Create object to send SMTP mail
        /// </summary>
        /// <param name="authEmail"></param>
        /// <param name="authPassword"></param>
        /// <param name="frmEmail"></param>
        /// <param name="frmName"></param>
        /// <param name="toEmail"></param>
        /// <param name="subject"></param>
        /// <param name="content"></param>
        public SendMail(string authEmail, string authPassword, string frmEmail, string frmName, string toEmail, string subject, string content)
        {
            this.authEmail = authEmail;
            this.authPassword = authPassword;
            this.content = content;
            this.frmEmail = frmEmail;
            this.frmName = frmName;
            this.subject = subject;
            this.toEmail = toEmail;
        }

        /// <summary>
        /// Send SMTP mail
        /// </summary>
        /// <returns>true if send otherwise false</returns>
        public bool Send()
        {
            SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
            smtp.Credentials = new NetworkCredential(authEmail, authPassword);
            smtp.EnableSsl = true;
            MailAddress addFrom = new MailAddress(frmEmail, frmName);
            MailAddress addTo = new MailAddress(toEmail);
            MailMessage email = new MailMessage(addFrom, addTo);
            email.IsBodyHtml = true;
            email.Subject = subject;
            email.Body = content;

            try
            {
                smtp.Send(email);
                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return false;
        }
    }
}
